<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-input v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="form.order_no" placeholder="订单流水号" />
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.status" placeholder="订单状态" style="width:100px;">
            <el-option label="处理中" value="0"> </el-option>
            <el-option label="成功" value="1"> </el-option>
            <el-option label="失败" value="2"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期" :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <!--<el-button type="primary" plain @click="onExport">导出</el-button>-->
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="merchant_id" label="商户ID" align="center"/>
          <el-table-column prop="merchant_name" label="商户名称" align="center" width="150" />
          <el-table-column prop="bill_no" label="订单号" align="center"/>
          <el-table-column prop="coin_name" label="币种" align="center" />
          <el-table-column prop="amount" label="购买数量" align="center"/>
          <el-table-column prop="trade_type" label="交易类型" align="center"/>
          <el-table-column prop="auditor" label="审核人" align="center"/>
          <el-table-column prop="audit_status" label="审核状态" align="center"/>
          <el-table-column prop="audit_time" label="审核时间" align="center" :formatter="formatterTime"/>
          <el-table-column prop="description" label="备注" align="center"/>
          <el-table-column prop="createtime" label="下单时间" align="center" :formatter="formatterTime" width="180"/>
          <el-table-column label="操作" align="center" fixed="right" width="100">
            <template #default="scope">
              <el-button
                type="primary"
                plain
                v-if="scope.row.status != 1"
                @click="handleReview(scope.$index, scope.row)"
                >审核</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <ReviewForm
      @cancel="reviewCancel"
      @closeDialog="reviewCloseDialog"
      @submitForm="reviewSubmit"
      :dialogFormVisible="reviewFormVisible"
      :dialogForm="reviewForm"
      :btnLoading="reviewBtnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, } from "vue";
import { formatterTime, timestamp, pickerDefaultTime } from "@/utils/utils";
import { usdmListApi, usdmEditApi } from "@/api/payServerse/wallet";
import ReviewForm from "./components/reviewForm.vue";

const tableData = ref([]);
const loading = ref(false);
const formData = {
  order_no: "",
  merchant_id: "",
  timeData: [],
  status: "",
};

const form = reactive({ ...formData });
// 审核
const reviewFormVisible = ref(false);
const reviewBtnLoading = ref(false);

const reviewFormData = {
  order_no: "",
  result: "",
};
const reviewForm = reactive({ ...reviewFormData });
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    order_no: form.order_no,
    merchant_id: form.merchant_id,
    status: form.status,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};

const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await usdmListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 取消
const reviewCancel = () => {
  reviewFormVisible.value = false;
  Object.assign(reviewForm, reviewFormData);
};
// 关闭
const reviewCloseDialog = () => {
  reviewFormVisible.value = false;
  Object.assign(reviewForm, reviewFormData);
};
// 提交
const reviewSubmit = async (param) => {
  try {
    reviewBtnLoading.value = true;
    const res = await usdmEditApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    reviewFormVisible.value = false;
    reviewBtnLoading.value = false;
    getTbaleData();
  } catch (error) {
    reviewBtnLoading.value = false;
    reviewFormVisible.value = false;
  }
};
// 审核操作
const handleReview = async (index, row) => {
  Object.assign(reviewForm, {
    order_no: row.bill_no,
  });
  reviewFormVisible.value = true;
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
